<template>
  <div>
    <h1>人员列表</h1>
    <h3>列表中第一个人的名字为:{{ getFirstName }}</h3>
    <input type="text" placeholder="输入添加的人员" v-model="name" />
    <button @click="add">添加</button>
    <button @click="addWang(name)">添加一个姓王的人</button>
    <button @click="addPersonFromServe">从服务器添加一个人</button>
    <ul>
      <li v-for="item in $store.state.personOptions.personList" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <h3 style="color: red">上方组件的求和为{{ sum }}</h3>
  </div>
</template>

<script>
import { nanoid } from "nanoid";
import { mapActions } from "vuex";
export default {
  name: "Persons",
  data() {
    return {
      name: "",
    };
  },
  methods: {
    add() {
      const personObj = { id: nanoid(), name: this.name };
      this.$store.commit("personOptions/ADD_PERSON", personObj);
      this.name = "";
    },
    addWang() {
      const personObj = { id: nanoid(), name: this.name };
      this.$store.dispatch("personOptions/addPersonWang", personObj);
      this.name = "";
    },
    ...mapActions("personOptions", {
      addPersonFromServe: "addPersonFromServe",
    }),
  },
  computed: {
    sum() {
      return this.$store.state.countOptions.sum;
    },
    getFirstName() {
      return this.$store.getters["personOptions/getFirstName"];
    },
  },
};
</script>

<style>
</style>